package com.tange.module.device.info.services;

import com.tange.core.backend.service.response.RxResponse;
import com.tange.module.device.info.DeviceInfoServiceManager;
import com.tg.appcommon.android.TGLog;
import com.tg.data.http.entity.DeviceServiceStatusBean;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 设备服务查询
 */
public class DeviceServiceQueryServiceImpl implements DeviceServiceQueryService {

    private static final String TAG = DeviceInfoServiceManager.TAG + "Services";

    public Observable<RxResponse<HashMap<String, DeviceServiceStatusBean>>> query(List<String> uuidList) {
        return Observable.create(emitter -> {
            query(uuidList, new DeviceServiceQueryListener() {
                @Override
                public void onSuccess(HashMap<String, DeviceServiceStatusBean> serviceStatusBeanHashMap) {
                    if (emitter == null || emitter.isDisposed()) {
                        return;
                    }

                    RxResponse rxResult = new RxResponse();
                    rxResult.content = serviceStatusBeanHashMap;
                    rxResult.isSuccess = true;

                    emitter.onNext(rxResult);
                }

                @Override
                public void onFailed(String msg) {
                    if (emitter == null || emitter.isDisposed()) {
                        return;
                    }

                    RxResponse rxResult = new RxResponse();
                    rxResult.isSuccess = false;
                    rxResult.errorMsg = msg;
                    emitter.onNext(rxResult);
                }
            });
        });
    }

    private void query(List<String> uuidList, DeviceServiceQueryListener listener) {

        if (uuidList == null || uuidList.size() < 1) {
            TGLog.i(TAG, "[query] uuidList = EMPTY");
            if (listener != null) {
                listener.onFailed("uuidList = EMPTY");
            }
            return;
        }

        TGLog.i(TAG, "[query] uuidList = "+ uuidList);

        final HashMap map = new HashMap();
        final StringBuilder sb = new StringBuilder();
        for (String str : uuidList) {
            sb.append(str).append(",");
        }
        map.put("uuid", sb.toString());

        TGLog.i(TAG, "[query] params = " + map);

        TGHttp.getInstance().getDeviceService(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<HashMap<String, DeviceServiceStatusBean>>() {
                    @Override
                    protected void onSuccess(final HashMap<String, DeviceServiceStatusBean> resp) {
                        TGLog.i(TAG, "[query][onSuccess] resp = " + resp);
                        if (listener != null) {
                            listener.onSuccess(resp);
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        String msg = "errorCode = " + errorCode + " , errorInfo = " + errorInfo;
                        TGLog.i(TAG, "[query][onResponseError] " + msg);
                        if (listener != null) {
                            listener.onFailed(msg);
                        }
                    }

                    @Override
                    protected void onOtherError(String error) {
                        TGLog.i(TAG, "[query][onOtherError] " + error);
                        if (listener != null) {
                            listener.onFailed(error);
                        }
                    }
                });
    }
}
